package com.zls.service.impl;

import com.zls.mapper.ProjectMapper;
import com.zls.page.PageResult;
import com.zls.page.QueryPageBean;
import com.zls.pojo.ProAndDevCon;
import com.zls.pojo.Project;
import com.zls.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class ProjectServiceImpl implements ProjectService {
    @Autowired
    private ProjectMapper projectMapper;
    @Override
    public List<Project> findAll() {
        return projectMapper.findAll();
    }

    //分页查询
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageResult pageResult = new PageResult();
        Integer currentPage = queryPageBean.getCurrentPage();//当前页
        Integer pageSize = queryPageBean.getPageSize();//每页大小
        String queryString = queryPageBean.getQueryString();//查询条件
        //total总记录数
        List<Project> page = projectMapper.findPage((currentPage - 1) * pageSize, pageSize,queryString);
        //rows  当前页的数据
        Long count = projectMapper.count(queryString);
        pageResult.setRows(page);
        pageResult.setTotal(count);
        return pageResult;
    }

    //增加
    @Transactional
    @Override
    public int addProject(Project project) {
        project.setUpdatetime(new Date());
        int i = projectMapper.addProject(project);
        return i;
    }

    //根据pid查询
    @Override
    public Project findById(int pid) {
        return projectMapper.findByid(pid);
    }

    //逻辑删除
    @Transactional
    @Override
    public int deleteProject(int pid) {
        int i = projectMapper.deleteProject(pid);
        return i;
    }
    //修改
    @Transactional
    @Override
    public int editProject(Project project) {
        project.setUpdatetime(new Date());
        int i = projectMapper.updateProject(project);
        return i;
    }

    @Override
    @Transactional
    public void bindDid(String did, int[] pids) {
        if (pids!=null && pids.length>0){
            for (int pid : pids) {
                projectMapper.bindDid(did,pid);
            }
        }
    }

    @Override
    public void bindCid(String cid, int[] pids) {
        if (pids!=null && pids.length>0){
            for (int pid : pids) {
                projectMapper.bindCid(cid,pid);
            }
        }
    }

    //根据did查询
    @Override
    public int findByDid(String did) {
        int i = projectMapper.selectByDid(did);
        return i;
    }
    //根据did查询集合
    @Override
    public List<Project> findByDids(String did) {
        return projectMapper.selectByDids(did);
    }

    //根据cid查询
    @Override
    public int findByCid(String cid) {
        int i = projectMapper.selectByCid(cid);
        return i;
    }
    //根据did查询集合
    @Override
    public List<Project> findByCids(String cid) {
        return projectMapper.selectByCids(cid);
    }

    @Override
    @Transactional
    public int deleteByDid(String did) {
        int i = projectMapper.deleteByDid(did);
        return i;
    }

    @Override
    @Transactional
    public int deleteByCid(String cid) {
        int i = projectMapper.deleteByCid(cid);
        return i;
    }

    @Override
    public ProAndDevCon index(int pid) {
        Project byid = projectMapper.findByid(pid);
        ProAndDevCon proAndDevCon=new ProAndDevCon();
        if (byid!=null){
            proAndDevCon.setPid(pid);
            if (byid.getPname()!=null){
                proAndDevCon.setPname(byid.getPname());
            }
            if (byid.getStarttime()!=null){
                proAndDevCon.setStarttime(byid.getStarttime());
            }
            if (byid.getEndtime()!=null){
                proAndDevCon.setEndtime(byid.getEndtime());
            }
            if (byid.getDid()!=null){
                proAndDevCon.setDid(byid.getDid());
            }
            if (byid.getCid()!=null){
                proAndDevCon.setCid(byid.getCid());
            }
            if (byid.getDeveloper()!=null && byid.getDeveloper().getDname()!=null){
                proAndDevCon.setDname(byid.getDeveloper().getDname());
            }
            if (byid.getContractor()!=null && byid.getContractor().getCname()!=null){
                proAndDevCon.setCname(byid.getContractor().getCname());
            }
        }
        return proAndDevCon;
    }
}
